package cn.edu.nju.setting;

import cn.edu.nju.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class SettingActivity extends Activity {
	public static final String RULE_ITEM_POSITION = "rule_item_position";
	private ListView ruleList;
	private FilterRuleListAdapter listAdapter;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        
        //init widget
        initAddRuleButton();
        initRuleList();
	}
	
	@Override
	public void onResume(){
		super.onResume();
		SettingProvider provider = SettingProvider.getInstance(this);
		if(provider.getHasRuleDeleted()){
			listAdapter.notifyDataSetChanged();
			provider.setHasRuleDeleted(false);
		} 
		else if(provider.getHasRuleAdded()){
			listAdapter.notifyDataSetChanged();
			provider.setHasRuleAdded(false);
		} else{
			listAdapter.updateInvalidView();
		}
	}
	
	private void initAddRuleButton(){
		Button addRuleButton = (Button) findViewById(R.id.add_rule_button);
		addRuleButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				Intent intent = new Intent(SettingActivity.this, CreateRuleActivity.class);
				startActivity(intent);
			}
		});		
	}
	
	private void initRuleList(){
		ruleList = (ListView)findViewById(R.id.filter_rule_list);
		listAdapter = new FilterRuleListAdapter(this);
		ruleList.setAdapter(listAdapter);
	}
}
